#-------------------------------------------------------------------------------
# Name:        wdfile
# Purpose:
#
# Author:     SUPER_紫电  QQ,911344755
#
# Created:    18/02/2023
# Copyright:  (c) SUPER_紫电 2022 All rights reserved.
# Licence:    <Modified BSD License>
#-------------------------------------------------------------------------------

import taskfile, vsc, vsctype, common

overlay = [0x10, 0x5C, 0x11, 0x13, 0x80, 0x1B]

pst_pstdata = [0xC3, 0xC4, 0xC5, 0xC8, 0x2401, 0x2403, 0x2404, 0x2407,
    0x2408, 0x2409, 0x2414, 0x2420, 0x2909, 0x290B]

drm_logs = [0x24, 0x25, 0x26]

zone_table = [0x03]

servo_logs = [0x49, 0x4A, 0xB5, 0x1100, 0x1101, 0x1121]

defects_list = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36]

defects_logs = [0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xF0, 0xF1, 0x3D08, 0x3D09]

pe_data  = [0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xF2, 0xF3]

selftest_logs = [0xE6, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE]

ibi_logs = [0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x97, 0x99, 0xA5, 0xC0, 0xCA,
    0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0x80FC, 0x80FE]
ibi_logs.extend([x for x in range(0x8000,0x806A,1)])

arco_logs_0xc4 = [0xC1, 0x96, 0xC7, 0x9C, 0xA0]
arco_logs_0xc4.extend([x for x in range(0x2000,0x2008,1)])
arco_logs_0xc4.extend([x for x in range(0x2020,0x2028,1)])
arco_logs_0xc4.extend([x for x in range(0x2040,0x2048,1)])
arco_logs_0xc4.extend([x for x in range(0x2050,0x2058,1)])
arco_logs_0xc4.extend([x for x in range(0x2060,0x2068,1)])

arco_logs_0xc3 = [0xC6]
arco_logs_0xc3.extend([x for x in range(0x2030,0x2038,1)])

arco_logs_0xc8 = [0xC9]
arco_logs_0xc8.extend([x for x in range(0x2010,0x2018,1)])

arco_logs_0x2909 = [x for x in range(0x20B0,0x20B8,1)]

arco_logs_0x290b = [x for x in range(0x20C0,0x20C8,1)]

arco_logs_0x2403 = [x for x in range(0x2630,0x263A,1)]

arco_logs_0x2404 = [x for x in range(0x2640,0x264A,1)]

arco_logs_0x2407 = [x for x in range(0x2670,0x267A,1)]

arco_logs_0x2408 = [x for x in range(0x2680,0x268A,1)]

arco_logs_0x2409 = [x for x in range(0x2690,0x269A,1)]

arco_logs_0x2414 = [x for x in range(0x26E0,0x26EA,1)]

arco_logs_0x2420 = [x for x in range(0x2640,0x264A,1)]

init = [0x6F, 0x03, 0x24, 0x25, 0x26, 0x49, 0x4A, 0xB5, 0x1100, 0x1101, 0x1121]

def init_fw(dcm=1, fmt_type=0):
    entry_list = vsc.get_res_entry()
    files = []
    files.extend(overlay)
    files.extend(pst_pstdata)
    for file_id in files:
        for file_entry in entry_list:
            if file_entry.FileID == file_id:
                if not vsc.rdfl(file_id):
                    return False
                if not vsc.svbin('%s\\%X.rpm' % (common.workdir, file_id)):
                    return False
    for file_id in init:
        for file_entry in entry_list:
            if file_entry.FileID == file_id:
                if not vsc.file_zero(file_id):
                    return False
    if not vsc.init_config:
        if not vsc.init2(True):
            return False
    actions = [
        vsc.file_zero(0x6F),
        vsc.file_zero(0x03),
        vsc.clr_factory_file,
        vsc.init_adp,
        vsc.init_odtaaws,
        vsc.init_mrr,
        vsc.init_rro,
        vsc.rst_err_rate,
        vsc.clr_dbs_all,
        vsc.clr_drm_log(1),
        vsc.clr_drm_section(0x100),
        vsc.clr_drm_section(0x200),
        vsc.clr_drm_section(0x300),
        vsc.clr_drm_section(0x400),
        vsc.clr_drm_section(0x500),
        vsc.clr_drm_section(0x600),
        vsc.clr_drm_section(0x700),
        vsc.clr_drm_section(0x800),
        vsc.clr_drm_section(0x900),
    ]
    for x in actions:
        if not x:
            return False
    vsc.init_region()
    if not vsc.spindown_reset():
        return False
    if not taskfile.wait_not_busy(30):
        return False
    if not vsc.ldovl(common.workdir):
        return False
    if not vsc.download_stfl(1):
        return False
    log_file = []
    log_file.extend(defects_list)
    log_file.extend(defects_logs)
    log_file.extend(pe_data)
    log_file.extend(selftest_logs)
    log_file.extend(ibi_logs)
    file_entry = vsctype.File_Entry()
    if vsc.file_find(0xC4, file_entry, entry_list):
        log_file.extend(arco_logs_0xc4)
    if vsc.file_find(0xC3, file_entry, entry_list):
        log_file.extend(arco_logs_0xc3)
    if vsc.file_find(0xC8, file_entry, entry_list):
        log_file.extend(arco_logs_0xc8)
    if vsc.file_find(0x2909, file_entry, entry_list):
        log_file.extend(arco_logs_0x2909)
    if vsc.file_find(0x290B, file_entry, entry_list):
        log_file.extend(arco_logs_0x290b)
    if vsc.file_find(0x2403, file_entry, entry_list):
        log_file.extend(arco_logs_0x2403)
    if vsc.file_find(0x2404, file_entry, entry_list):
        log_file.extend(arco_logs_0x2404)
    if vsc.file_find(0x2407, file_entry, entry_list):
        log_file.extend(arco_logs_0x2407)
    if vsc.file_find(0x2408, file_entry, entry_list):
        log_file.extend(arco_logs_0x2408)
    if vsc.file_find(0x2409, file_entry, entry_list):
        log_file.extend(arco_logs_0x2409)
    if vsc.file_find(0x2414, file_entry, entry_list):
        log_file.extend(arco_logs_0x2414)
    if vsc.file_find(0x2420, file_entry, entry_list):
        log_file.extend(arco_logs_0x2420)
    for file_id in log_file:
        for file_entry in entry_list:
            if file_entry.FileID == file_id:
                if not vsc.file_zero(file_id):
                    return False
    if not vsc.fmt_select_set_dcm(dcm, fmt_type):
        return False
    if not taskfile.wait_not_busy(30):
        return False
    return taskfile.rstat_bitclr(0)
